package com.caigou.modules.famous.ui.challenge.fragment

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.caigou.comm.base.bus.BusKey
import com.caigou.comm.base.bus.LiveDataBus
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.ext.observe
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.famous.R
import com.caigou.modules.famous.adpter.RewardRecordItemAdapter
import com.caigou.modules.famous.bean.MyChallengeItemBean
import com.caigou.modules.famous.databinding.FragmentRewardRecordBinding
import com.caigou.modules.famous.ui.challenge.FamousChallengeActivity
import com.caigou.modules.famous.viewmodel.ChallengeViewModel
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/12/10
 */
@AndroidEntryPoint
class RewardRecordFragment : BaseFragment() {

    private var _binding: FragmentRewardRecordBinding? = null
    private val binding get() = _binding!!

    private val challengeViewModel: ChallengeViewModel by viewModels()

    private val mAdapter: RewardRecordItemAdapter by lazy {
        RewardRecordItemAdapter(mutableListOf())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentRewardRecordBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.rewardRecordSwipeRefreshLayout.run {
            setColorSchemeColors(resources.getColor(R.color.caigou_theme_color))
            setOnRefreshListener {
                fetchRewardRecordList()
            }
        }

        binding.rewardRecordRecyclerView.run {
            layoutManager = LinearLayoutManager(requireActivity())
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(SpaceItemDecoration(requireActivity()))
        }

        mAdapter.setOnItemClickListener { adapter, view, position ->
//            ARouter.getInstance().build(RouterPath.Forum.PAGE_POST).navigation()
        }

        LiveDataBus.get().with(BusKey.SUBSCRIBE_TYPE_CHANGE, Int::class.java).observe(this) {
            fetchRewardRecordList()
        }
    }

    override fun lazyLoad() {
        fetchRewardRecordList()
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    override fun observeViewModel() {
        super.observeViewModel()
        observe(challengeViewModel.rewardRecordListLiveData, ::handleGetRewardRecordList)
    }

    private fun handleGetRewardRecordList(result: Resource<MutableList<MyChallengeItemBean>>) {
        when (result) {
            is Resource.Loading -> {
                mDialog.show()
            }
            is Resource.DataError -> {
                mDialog.dismiss()
                binding.rewardRecordSwipeRefreshLayout.isRefreshing = false
                if (!TextUtils.isEmpty(result.errorMsg)) {
                    showToast(result.errorMsg)
                } else {
                    showToast("获取领取记录信息异常")
                }
            }
            is Resource.Success -> {
                mDialog.dismiss()
                binding.rewardRecordSwipeRefreshLayout.isRefreshing = false
                if (result.data == null) {
                    return
                }
                mAdapter.setList(result.data!!)
            }
        }
    }

    private fun fetchRewardRecordList() {
        val type = (requireActivity() as FamousChallengeActivity).famousType
        challengeViewModel.getRewardRecordList(type)
    }
}